package com.cuitrip.business.conversation;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.cuitrip.app.rong.RongCloudEvent;
import com.cuitrip.business.order.detail.OrderSubFragment;
import com.cuitrip.business.order.model.OrderItem;
import com.lab.logtrack.AppLog;
import com.lab.utils.LogHelper;

import io.rong.imkit.fragment.ConversationFragment;
import io.rong.imlib.model.Conversation;

/**
 * Created by baziii on 15/9/2.
 */
public class CtConversationFragment extends ConversationFragment implements OrderSubFragment{
    public static final String ORDER_ID = "CtConversationFragment.ORDER_ID";
    //传这个参数真是太蛋疼叻
    public static final String RECEIVE_ID = "CtConversationFragment.RECEIVE_ID";

    InputMethodManager manager;
    private String target;
    private OrderItem orderItem;
    private BroadcastReceiver mOrderStatusChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (RongCloudEvent.ACTION_REFRESH_HAS_READ.equals(intent.getAction())) {
                refreshAsRead();
            }
        }
    };

    public static CtConversationFragment newInstance(String orderId, boolean hasOldVersionMessage,
                                                     String otherId) {
        Bundle args = new Bundle();
        args.putString(ORDER_ID, orderId);
        args.putString(RECEIVE_ID, otherId);
        CtConversationFragment fragment = new CtConversationFragment();
        fragment.setArguments(args);
        LogHelper.e("omg", "newInstance  " + hasOldVersionMessage);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        IntentFilter filter = new IntentFilter(RongCloudEvent.ACTION_REFRESH_HAS_READ);
        getContext().registerReceiver(mOrderStatusChangedReceiver, filter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        getContext().unregisterReceiver(mOrderStatusChangedReceiver);
        super.onDestroyView();
    }

    public void setUri(ApplicationInfo applicationInfo, OrderItem orderItem){
        target = orderItem.getTargetId();
        this.orderItem = orderItem;
        AppLog.d("build fragment   target id" + target);
        String title = orderItem.getServiceName();
        Uri uri = Uri.parse("rong://" + applicationInfo.packageName).buildUpon().appendPath("conversation")
                .appendPath(Conversation.ConversationType.DISCUSSION.getName().toLowerCase())
                .appendQueryParameter("targetId", target).appendQueryParameter("title", title).build();
        setUri(uri);
        refreshAsRead();
    }

    private void refreshAsRead() {
        if (orderItem != null) {
            RongCloudEvent.getInstance().markAsRead(target, orderItem.getOid());
        }
    }

    @Override
    public void onLeaveMe(Activity activity) {

        if (activity != null && activity.getCurrentFocus() != null){
            manager  = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            try{
                manager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }catch (Exception e){
                //防止出键盘错误
            }
        }

    }


}
